<!--  -->
<template>
  <div id="main">
    <h2>财务管理</h2>
    <div id="search-card">
      <el-form ref="search" :model="search" :inline="true" :rules="rules">
        <el-form-item label="统计方式" prop="unit">
          <el-select v-model="search.unit" placeholder="请选择" style="width:100px">
            <el-option label="周" value="1" />
            <el-option label="月" value="2" />
            <el-option label="季" value="3" />
            <el-option label="年" value="4" />
          </el-select>
        </el-form-item>
        <el-form-item prop="date">
          <el-date-picker v-model="search.date" value-format="yyyy-MM-dd" type="date" placeholder="请选择日期" style="width:140px" />
        </el-form-item>

        <el-form-item>
          <el-button type="primary" style="margin-left:10px" @click="submit('search')">提交</el-button>
          <el-button icon="el-icon-refresh" circle @click="refresh()" />
        </el-form-item>
      </el-form>
    </div>
    <!-- <div id="search-card">
      <div id="search-content">
        <el-form v-model="search" :inline="true">

          <el-form-item label="昵称">
            <el-input v-model="search.username" />
          </el-form-item>

          <el-form-item label="所属机构">
            <el-select v-model="search.orgId" placeholder="请选择">
              <el-option
                v-for="o in org"
                :key="o.orgId"
                :value="o.orgId"
                :label="o.orgName"
              />
            </el-select>
          </el-form-item>

          <el-form-item label="用户角色">
            <el-select v-model="search.userGroup" placeholder="请选择">
              <el-option key="1" label="系统管理员" value="1" />
              <el-option key="2" label="代理人" value="2" />
              <el-option key="3" label="机构管理员" value="3" />
              <el-option key="4" label="分支机构管理员" value="4" />
            </el-select>
          </el-form-item>

          <el-form-item>
            <el-button type="primary" style="margin-left:10px" @click="conditionQuery">查找</el-button>
            <el-button icon="el-icon-refresh" circle @click="fetchData()" />
          </el-form-item>

        </el-form>
      </div>

    </div> -->

    <div id="table-area">
      <el-table
        v-loading="listLoading"
        :data="list.slice((currentPage-1)*pagesize,currentPage*pagesize)"
        element-loading-text="Loading"
        border
        fit
        highlight-current-row
      >

        <el-table-column
          label="序号"
          type="index"
          width="80"
          align="center"
        >
          <template scope="scope">
            <span>{{ (currentPage - 1) * pagesize + scope.$index + 1 }}</span>
          </template>
        </el-table-column>

        <el-table-column
          align="center"
          prop="agentName"
          label="代理人"
        />

        <el-table-column
          align="center"
          prop="count"
          label="订单数量"
        />

        <el-table-column
          align="center"
          prop="money"
          label="订单金额"
        />

        <el-table-column
          align="center"
          prop="countByStage"
          label="分期订单数量"
        />

        <el-table-column
          align="center"
          prop="moneyByStage"
          label="分期订单金额"
        />

      </el-table>

      <div id="pages">
        <el-pagination
          :current-page="currentPage"
          :page-sizes="[5,10]"
          :page-size="pagesize"
          layout="total, sizes,prev, pager, next"
          :total="list.length"
          prev-text="上一页"
          next-text="下一页"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>
  </div>
</template>

<script>
// 这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
// 例如：import 《组件名称》 from '《组件路径》';
import { countFinance } from '@/api/statistic'
export default {
// import引入的组件需要注入到对象中才能使用
  components: {},
  data() {
    return {
      currentPage: 1, // 默认显示页面为1
      pagesize: 5, //    每页的数据条数
      list: [],
      listLoading: false,
      search: {
        date: ''
      },
      rules: {
        date: [
          { required: true, message: '请选择日期', trigger: 'blur' }
        ]
      }
    }
  },
  // 监听属性 类似于data概念
  computed: {},
  // 监控data中的数据变化
  watch: {},
  // 生命周期 - 创建完成（可以访问当前this实例）
  created() {
    this.fetchData()
  },
  // 生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {

  },
  beforeCreate() {}, // 生命周期 - 创建之前
  beforeMount() {}, // 生命周期 - 挂载之前
  beforeUpdate() {}, // 生命周期 - 更新之前
  updated() {}, // 生命周期 - 更新之后
  beforeDestroy() {}, // 生命周期 - 销毁之前
  destroyed() {}, // 生命周期 - 销毁完成
  activated() {},
  // 方法集合
  methods: {
    // 每页下拉显示数据
    handleSizeChange: function(size) {
      this.pagesize = size
    },
    // 点击第几页
    handleCurrentChange: function(currentPage) {
      this.currentPage = currentPage
    },
    handleClick(row) {
      console.log(row)
    },
    fetchData() {
      this.listLoading = true
      const nowDate = new Date()
      const date = {
        year: nowDate.getFullYear(),
        month: nowDate.getMonth() < 9 ? '0' + (nowDate.getMonth() + 1) : nowDate.getMonth() + 1,
        date: nowDate.getDate() <= 9 ? '0' + nowDate.getDate() : nowDate.getDate()
      }
      const systemDate = date.year + '-' + date.month + '-' + date.date
      console.log(systemDate)
      countFinance(1, systemDate).then((resp) => {
        this.list = resp.obj
        for (var i = 0; i < this.list.length; i++) {
          this.list[i].money = this.list[i].money / 100
          this.list[i].moneyByStage = this.list[i].moneyByStage / 100
        }
        this.listLoading = false
      })
    },
    submit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.listLoading = true
          console.log(this.search.date)
          countFinance(this.search.unit, this.search.date).then((resp) => {
            this.list = resp.obj
            for (var i = 0; i < this.list.length; i++) {
              this.list[i].money = this.list[i].money / 100
              this.list[i].moneyByStage = this.list[i].moneyByStage / 100
            }
            this.listLoading = false
          })
          return true
        } else {
          this.$notify({
            title: '请正确填写',
            type: 'error'
          })
          return false
        }
      })
    },
    refresh() {
      this.search = []
      this.fetchData()
    }
  } // 如果页面有keep-alive缓存功能，这个函数会触发
}
</script>
<style scoped>
#main{
  margin-left: 5px;
  margin-right: 5px;
}
#table-area{
  margin-left: 10px;
  margin-right: 10px;
}
#search-card {
  line-height: 0px;
  margin-left: 10px;
  text-align: left;
  width: 100%;
  height: 70px;
  overflow-x: hidden;
}
</style>
